//
// Created by yangchao on 2022/5/6.
// 49. 字母异位词分组: https://leetcode-cn.com/problems/group-anagrams/
//

#include <string>
#include <unordered_map>
#include <algorithm>
#include <vector>
using namespace std;

//怎么判断是否有重复呢: 排序后看是否相同！！！
class GroupAnagrams{
public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) {
        vector<vector<string>> res;
        unordered_map<string,vector<string>> mp;
        for (string& str:strs) {
            string key = str;
            //将串排序后便于作为同一个键
            sort(key.begin(),key.end());
            //将相同键值对的字符放入vector容器中
            mp[key].emplace_back(str);
        }
        //取出相同键值的vector
        for(auto it = mp.begin(); it != mp.end(); it++) {
            res.push_back(it->second);
        }
        return res;
    }
};
